package com.aaa.marketresearch.dataresource.domain;

import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.*;
import java.io.PrintWriter;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Nail
 */

@Entity
@Table(name = "\"FAMILY_MEMBER\"")
public class FamilyMember implements Serializable {

    private static final String BIRTHDAY_DATE_FORMAT = "dd-MM-yyyy";
    private static final DateFormat birthdayDateFormat = new SimpleDateFormat(BIRTHDAY_DATE_FORMAT);
    private static final Logger log = LoggerFactory.getLogger(FamilyMember.class);

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "\"FAMILY_MEMBER_id_seq\"")
    @SequenceGenerator(name = "\"FAMILY_MEMBER_id_seq\"", sequenceName = "\"FAMILY_MEMBER_id_seq\"")
    @Column(name = "\"id\"", nullable = false)
    private Integer id;

    @Column(name = "\"familyId\"", nullable = false)
    private Integer familyId;

    @Column(name = "\"name\"", nullable = false)
    private String name;

    @Column(name = "\"gender\"")
    private String gender;

    @Column(name = "\"profession\"")
    private String profession;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "\"birthday\"")
    private Date birthday;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getFamilyId() {
        return familyId;
    }

    public void setFamilyId(Integer familyId) {
        this.familyId = familyId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void setWithJSONObject(JSONObject jObj) {
        Object obj;

        obj = jObj.get("familyId");
        if (obj != null) {
            this.familyId = ((Long)obj).intValue();
        }
        obj = jObj.get("name");
        if (obj != null) {
            this.name = (String)obj;
        }
        obj = jObj.get("gender");
        if (obj != null) {
            this.gender = (String)obj;
        }
        obj = jObj.get("profession");
        if (obj != null) {
            this.profession = (String)obj;
        }
        obj = jObj.get("birthday");
        if (obj != null) {
            String birthdayDate = (String)obj;
            try {
                this.birthday = birthdayDateFormat.parse(birthdayDate);
            } catch (ParseException ex) {
                log.error("setWithJSONObject birthday parse exception {}", ex);
            }
        }
    }

    public void printJSON(PrintWriter out) {
        out.print("{\"id\":");out.print(this.id);out.print(",");
        out.print("\"familyId\":");out.print(this.familyId);out.print(",");
        out.print("\"name\":\"");out.print(this.name);out.print("\",");
        out.print("\"gender\":\"");out.print(this.gender);out.print("\",");
        out.print("\"profession\":\"");out.print(this.profession);out.print("\",");
        String birthdayDate = birthdayDateFormat.format(this.birthday);
        out.print("\"birthday\":\"");out.print(birthdayDate);out.print("\"}");
    }

    public void printJSON(StringBuilder builder) {
        builder.append("{\"id\":").append(this.id).append(",");
        builder.append("\"familyId\":").append(this.familyId).append(",");
        builder.append("\"name\":\"").append(this.name).append("\",");
        builder.append("\"gender\":\"").append(this.gender).append("\",");
        builder.append("\"profession\":\"").append(this.profession).append("\",");
        String birthdayDate = birthdayDateFormat.format(this.birthday);
        builder.append("\"birthday\":\"").append(birthdayDate).append("\"}");
    }

}
